<?php


namespace App\Admin\Controllers;


use App\Models\Community;
use SmallRuralDog\Admin\Components\Attrs\SelectOption;
use SmallRuralDog\Admin\Components\Form\Checkbox;
use SmallRuralDog\Admin\Components\Form\InputNumber;
use SmallRuralDog\Admin\Components\Form\Select;
use SmallRuralDog\Admin\Components\Form\Upload;
use SmallRuralDog\Admin\Components\Grid\Image;
use SmallRuralDog\Admin\Controllers\AdminController;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;

class CommunityController extends AdminController
{

    public function grid()
    {
        $grid = new Grid(new Community());
        $grid->model()->with(['children']);
        $grid->column('title', '小区名称')->width(280);
        $grid->model()->where('parent_id', 0);
        $grid->tree();
        $grid->column('id', '小区编号')->width(180);
        $grid->column('community_key', '小区标识')->width(220);
        $grid->column('icon', '图标')->component(Image::make()->size(50, 50));


        $grid->toolbars(function (Grid\Toolbars $toolbars) {
        });

        return $grid;
    }

    public function form($isEdit = false)
    {
        $form = new Form(new Community());

        $form->item('parent_id', '上级菜单')->component(Select::make(0)->options(function () {
            return Community::query()->where('parent_id', 0)->orderBy('order')->get()->map(function ($item) {
                return SelectOption::make($item->id, $item->title);
            })->prepend(SelectOption::make(0, '顶级菜单'));
        }));
        $form->item('title', '社区名称')->required()->inputWidth(5);
        $form->item('community_key', '唯一标识')->required()->inputWidth(5);
        $form->item('icon', '图标')->component(Upload::make()->width(80)->height(80));
        $form->item('order', '排序')->required()->component(InputNumber::make(1));
        $form->item('status', '状态')->required()->component(Checkbox::make(1, "启用"));

        return $form;
    }
}
